﻿using System;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace TfsGiggle.Core
{
    internal class TfsTeamProjectCollectionImpl: ITfsTeamProjectCollection, IDisposable, IServiceProvider
    {
        private TfsTeamProjectCollection _tfsTeamProjectCollection;

        public TfsTeamProjectCollectionImpl(TfsConfigurationServer tfsConfigurationServer, Guid guid)
        {
            _tfsTeamProjectCollection = tfsConfigurationServer.GetTeamProjectCollection(guid);
        }

        public void EnsureAuthenticated()
        {
            _tfsTeamProjectCollection.EnsureAuthenticated();
        }

        private void Dispose(bool disposing)
        {
            if( disposing)
            {
                if (_tfsTeamProjectCollection != null)
                {
                    _tfsTeamProjectCollection.Dispose();
                    _tfsTeamProjectCollection = null;
                }
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        object IServiceProvider.GetService(Type serviceType)
        {
            if (serviceType == typeof(IVersionControlServer))
            {
                return new VersionControlServerImpl(_tfsTeamProjectCollection.GetService<VersionControlServer>());
            }
            throw new NotImplementedException(serviceType.ToString());
        }
    }
}
